import axios from 'axios'
import { Message } from 'element-ui'
// import nprogress from 'nprogress'
// import 'nprogress/nprogress.css'

// axios.defaults.withCredentials = true;
const service = axios.create({
    //baseURL: "http://api.tianapi.com/",
    //baseURL:'https://www.fastmock.site/mock/3ddb36ed19a06abb22b4c7660935eb99/forest/',
    timeout: 6000,
    // baseURL: '/api',
    withCredentials: true,
    crossDomain: true
  })

  // 请求拦截
service.interceptors.request.use(config => {
    if (!config.data) {
      config.data = {}
    }

    config.headers["X-Requested-With"] = 'XMLHttpRequest';
    config.headers["Content-Type"] = 'application/json;charset=UTF-8';
    const tokenValue = localStorage.getItem('tokenValue');
    config.headers.Authorization = tokenValue;
    // config.headers["withCredentials"] = 'true';
    // axios.defaults.withCredentials=true;

   // nprogress.start()
    console.log(config.data)
    console.log(tokenValue)
    return config;
  })

  // 响应拦截
service.interceptors.response.use(res => {
    if (res.data.code == 200) {
     // nprogress.done()
      return res.data;
    } else {
      Message.error(res.data.msg);
      console.log(res.data);
      return Promise.reject(res.data.msg);
    }
  
  }, err => {
  
    return Promise.reject(err);
  })

  //导出
  export default service;